package com.minarto.display.graphics {
	import flash.display.*;
	import flash.geom.Matrix;
	
	/**
	 * ...
	 * @author minarto
	 */
	public class DotLine 
	{
		private var _graphics:Graphics,
					_matrix:Matrix = new Matrix(),
					_colors:Array = [0, 0, 0, 0],
					_alphas:Array = [0, 0, 0, 0],
					_ratios:Array = [0, 0, 0, 0xFF],
					_px:Number = 0,
					_py:Number = 0,
					_dash:uint = 4,
					_spacing:uint = 4;
					
		
		public function setStyle($graphics:Graphics, $thickness:Number=NaN, $color:uint=0, $alpha:Number = 1, $pixelHinting:Boolean = false, $scaleMode:String = LineScaleMode.NONE, $caps:String = CapsStyle.NONE, $joints:String = JointStyle.ROUND, $miterLimit:Number = 3):void {
			_graphics = $graphics;
			
			_colors[0] = $color;
			_colors[1] = $color;
			_colors[2] = $color;
			_colors[3] = $color;
			
			_alphas[2] = $alpha;
			_alphas[3] = $alpha;
			
			if (_graphics) {
				_graphics.lineStyle($thickness, $color, $alpha, $pixelHinting, $scaleMode, $caps, $joints, $miterLimit);
			}
		}
		
		
		public function dotStyle($dash:uint = 4, $spacing:uint = 4):void {
			var n:uint;
			
			_dash = $dash;
			_spacing = $spacing;
			n = _dash / (_dash + _spacing) * 0xFF;
			
			_ratios[1] = n;
			_ratios[2] = n + 1;
		}
		
		
		public function moveTo($x:Number, $y:Number):void {
			if (!_graphics)	return;
			
			_px = $x;
			_py = $y;
			
			_graphics.moveTo(_px, _py);
		}
		
		
		public function lineTo($x:Number, $y:Number):void {
			var x:Number, y:Number, r:Number;
			
			if (!_graphics)	return;
			
			x = $x - _px;
			y = $y - _py;
			r = Math.atan2(y, x);
			y = _dash + _spacing;
			x = Math.cos(r) * y;
			y = Math.sin(r) * y;
			
			_matrix.createGradientBox(x, y, r, _px, _py);
			_graphics.lineGradientStyle(GradientType.LINEAR, _colors, _alphas, _ratios, _matrix, SpreadMethod.REPEAT, InterpolationMethod.LINEAR_RGB, 0);
			_graphics.lineTo($x, $y);
			
			_px = $x;
			_py = $y;
		}
	}

}